<?php
/**
 * 坤典科技-测试版模块小程序接口定义
 *
 * @author 说图谱网
 * @url http://www.shuotupu.com/
 */
defined('IN_IA') or exit('Access Denied');

class Kundian_farmModuleWxapp extends WeModuleWxapp {
	public function doPageTest(){
		global $_GPC, $_W;
		$errno = 0;
		$message = '返回消息';
		$data = array();
		return $this->result($errno, $message, $data);
	}
	public function doPageIndex(){
	    include 'inc/wxapp/index.inc.php';
    }
    public function doPageShop(){
	    include 'inc/wxapp/shop.inc.php';
    }
    public function doPageCart(){
	    include 'inc/wxapp/cart.inc.php';
    }
    public function doPageOrder(){
        include 'inc/wxapp/order.inc.php';
    }
    public function doPageGroup(){
        include 'inc/wxapp/group.inc.php';
    }
    public function doPageSign(){
        include 'inc/wxapp/sign.inc.php';
    }
    public function doPageIntegral(){
        include 'inc/wxapp/integral.inc.php';
    }
    public function doPageLand(){
        include 'inc/wxapp/land.inc.php';
    }
    public function doPageVet(){
        include 'inc/wxapp/vet.inc.php';
    }
    public function doPageSeed(){
        include 'inc/wxapp/seed.inc.php';
    }
    public function doPageAddress(){
        include 'inc/wxapp/address.inc.php';
    }
    public function doPageCoupon(){
        include 'inc/wxapp/coupon.inc.php';
    }
    public function doPageManager(){
        include 'inc/wxapp/manager.inc.php';
    }
    public function doPageManagerLand(){
        include 'inc/wxapp/managerLand.inc.php';
    }
    public function doPageManagerAnimal(){
        include 'inc/wxapp/managerAnimal.inc.php';
    }
    public function doPageWebThing(){
        include 'inc/wxapp/webThing.inc.php';
    }
    public function doPageDistribution(){
        include 'inc/wxapp/distribution.inc.php';
    }
    //商城支付
    public function doPageShopPay() {
        global $_GPC, $_W;
        //获取订单号，保证在业务模块中唯一即可
        $order_id = intval($_GPC['orderid']);
        $uniacid=$_GPC['uniacid'];
        $orderData=pdo_get('cqkundian_farm_shop_order',array('id'=>$order_id,'uniacid'=>$uniacid));
        //构造支付参数
        $order = array(
            'tid' => $orderData['order_number'],
            'user' => $_W['openid'], //用户OPENID
            'fee' => $orderData['total_price'], //金额
            'title' => '购物消费',
        );
        //生成支付参数，返回给小程序端
        $pay_params = $this->pay($order);
        if (is_error($pay_params)) {
            return $this->result(1, $pay_params['message']);
        }
        cache_write("kundian_farm_pay_notify_".$_W['openid'],1);
        return $this->result(0, '', $pay_params);
    }
    //组团商城支付
    public function doPageGroupPay() {
        global $_GPC, $_W;
        //获取订单号，保证在业务模块中唯一即可
        $order_id = intval($_GPC['orderid']);
        $uniacid=$_GPC['uniacid'];
        $orderData=pdo_get('cqkundian_farm_group_order',array('id'=>$order_id,'uniacid'=>$uniacid));
        //构造支付参数
        $order = array(
            'tid' => $orderData['order_number'],
            'user' => $_W['openid'], //用户OPENID
            'fee' => $orderData['total_price'], //金额
            'title' => $orderData['body'],
        );
        //生成支付参数，返回给小程序端
        $pay_params = $this->pay($order);
        if (is_error($pay_params)) {
            return $this->result(1, $pay_params['message']);
        }
        cache_write("kundian_farm_pay_notify_".$_W['openid'],2);
        return $this->result(0, '', $pay_params);
    }
    //动物领养支付
    public function doPageAnimalPay(){
        global $_GPC, $_W;
        //获取订单号，保证在业务模块中唯一即可
        $order_id = intval($_GPC['orderid']);
        $uniacid=$_GPC['uniacid'];
        $orderData=pdo_get('cqkundian_farm_animal_order',array('id'=>$order_id,'uniacid'=>$uniacid));
        //构造支付参数
        $order = array(
            'tid' => $orderData['order_number'],
            'user' => $_W['openid'], //用户OPENID
            'fee' => $orderData['total_price'], //金额
            'title' => $orderData['body'],
        );
        //生成支付参数，返回给小程序端
        $pay_params = $this->pay($order);
        if (is_error($pay_params)) {
            return $this->result(1, $pay_params['message']);
        }
        cache_write("kundian_farm_pay_notify_".$_W['openid'],3);
        return $this->result(0, '', $pay_params);
    }
    //宰杀寄送支付运费
    public function doPageAnimalSendPay(){
        global $_GPC, $_W;
        //获取订单号，保证在业务模块中唯一即可
        $order_id = intval($_GPC['orderid']);
        $uniacid=$_GPC['uniacid'];
        $orderData=pdo_get('cqkundian_farm_shop_order',array('id'=>$order_id,'uniacid'=>$uniacid));
        //构造支付参数
        $order = array(
            'tid' => $orderData['order_number'],
            'user' => $_W['openid'], //用户OPENID
            'fee' => $orderData['total_price'], //金额
            'title' => $orderData['body'],
        );
        //生成支付参数，返回给小程序端
        $pay_params = $this->pay($order);
        if (is_error($pay_params)) {
            return $this->result(1, $pay_params['message']);
        }
        cache_write("kundian_farm_pay_notify_".$_W['openid'],4);
        return $this->result(0, '', $pay_params);
    }
    //购买土地支付
    public function  doPageLandPay(){
        global $_GPC, $_W;
        //获取订单号，保证在业务模块中唯一即可
        $order_id = intval($_GPC['orderid']);
        $uniacid=$_GPC['uniacid'];
        $orderData=pdo_get('cqkundian_farm_land_order',array('id'=>$order_id,'uniacid'=>$uniacid));
        //构造支付参数
        $order = array(
            'tid' => $orderData['order_number'],
            'user' => $_W['openid'], //用户OPENID
            'fee' =>$orderData['total_price'], //金额
            'title' => $orderData['body'],
        );
        //生成支付参数，返回给小程序端
        $pay_params = $this->pay($order);
        if (is_error($pay_params)) {
            return $this->result(1, $pay_params['message']);
        }
        cache_write("kundian_farm_pay_notify_".$_W['openid'],5);
        return $this->result(0, '', $pay_params);
    }
    //购买种子支付
    public function  doPageSendPay(){
        global $_GPC, $_W;
        //获取订单号，保证在业务模块中唯一即可
        $order_id = intval($_GPC['orderid']);
        $uniacid=$_GPC['uniacid'];
        $orderData=pdo_get('cqkundian_farm_send_order',array('id'=>$order_id,'uniacid'=>$uniacid));
        //构造支付参数
        $order = array(
            'tid' => $orderData['order_number'],
            'user' => $_W['openid'], //用户OPENID
            'fee' => $orderData['total_price'], //金额
            'title' => $orderData['body'],
        );
        //生成支付参数，返回给小程序端
        $pay_params = $this->pay($order);
        if (is_error($pay_params)) {
            return $this->result(1, $pay_params['message']);
        }
        cache_write("kundian_farm_pay_notify_".$_W['openid'],6);
        return $this->result(0, '', $pay_params);
    }
    //摘取运费支付
    public function doPageSeedSendPay(){
        global $_GPC, $_W;
        //获取订单号，保证在业务模块中唯一即可
        $order_id = intval($_GPC['orderid']);
        $uniacid=$_GPC['uniacid'];
        $orderData=pdo_get('cqkundian_farm_shop_order',array('id'=>$order_id,'uniacid'=>$uniacid));
        //构造支付参数
        $order = array(
            'tid' => $orderData['order_number'],
            'user' => $_W['openid'], //用户OPENID
            'fee' => $orderData['total_price'], //金额
            'title' => $orderData['body'],
        );
        //生成支付参数，返回给小程序端
        $pay_params = $this->pay($order);
        if (is_error($pay_params)) {
            return $this->result(1, $pay_params['message']);
        }
        cache_write("kundian_farm_pay_notify_".$_W['openid'],7);
        return $this->result(0, '', $pay_params);
    }
    //积分运费支付
    public function doPageIntegralPay(){
        global $_GPC, $_W;
        //获取订单号，保证在业务模块中唯一即可
        $order_id = intval($_GPC['orderid']);
        $uniacid=$_GPC['uniacid'];
        $orderData=pdo_get('cqkundian_farm_integral_order',array('id'=>$order_id,'uniacid'=>$uniacid));
        //构造支付参数
        $order = array(
            'tid' => $orderData['order_number'],
            'user' => $_W['openid'], //用户OPENID
            'fee' => $orderData['send_price'], //金额 支付运费
            'title' => $orderData['body'],
        );
        //生成支付参数，返回给小程序端
        $pay_params = $this->pay($order);
        if (is_error($pay_params)) {
            return $this->result(1, $pay_params['message']);
        }
        cache_write("kundian_farm_pay_notify_".$_W['openid'],8);
        return $this->result(0, '', $pay_params);
    }

    /**
     * 支付回调
     * @param $log
     */
    public function payResult($log){
        if ($log['result']=='success') {
            //载入日志函数
            load()->func('logging');
            //记录数组数据
            $order_id = $log['tid'];
            $uniontid = $log['uniontid'];
            $fee = $log['fee'];
            $user=$log['user'];
            $remark=cache_load('kundian_farm_pay_notify_'.$user);
            if($remark==1){  //普通商城支付回调
                $update_save=array(
                    'status'=>1,
                    'pra_price'=>$fee,
                    'pay_time'=>time(),
                    'pay_method'=>'微信支付',
                    'uniontid' => $uniontid,
                );
                $res=pdo_update('cqkundian_farm_shop_order',$update_save,array('order_number'=>$order_id,'uniacid'=>$log['uniacid']));
            }elseif ($remark==2){   //组团回调
                $update_save=array(
                    'status'=>6,
                    'pra_price'=>$fee,
                    'pay_time'=>time(),
                    'pay_method'=>'微信支付',
                    'uniontid' => $uniontid,
                );
                $res=pdo_update('cqkundian_farm_group_order',$update_save,array('order_number'=>$order_id,'uniacid'=>$log['uniacid']));
            }elseif ($remark==3){  //畜牧认养
                $update_save=array(
                    'status'=>1,      //已支付
                    'pra_price'=>$fee,
                    'pay_method'=>'微信支付',
                    'uniontid' => $uniontid,
                );
                $res=pdo_update('cqkundian_farm_animal_order',$update_save,array('order_number'=>$order_id,'uniacid'=>$log['uniacid']));
            }elseif ($remark==4){  //宰杀寄送
                $update_save=array(
                    'status'=>1,
                    'pra_price'=>$fee,
                    'pay_time'=>time(),
                    'pay_method'=>'微信支付',
                    'uniontid' => $uniontid,
                );
                $res=pdo_update('cqkundian_farm_shop_order',$update_save,array('order_number'=>$order_id,'uniacid'=>$log['uniacid']));
            }elseif ($remark==5){  //土地购买支付
                $update_save=array(
                    'status'=>1,
                    'pra_price'=>$fee,
                    'pay_time'=>time(),
                    'pay_method'=>'微信支付',
                    'uniontid' => $uniontid,
                );
                $res=pdo_update('cqkundian_farm_land_order',$update_save,array('order_number'=>$order_id,'uniacid'=>$log['uniacid']));
            }elseif ($remark==6){       //种子购买
                $update_save=array(
                    'status'=>1,
                    'pra_price'=>$fee,
                    'pay_time'=>time(),
                    'pay_method'=>'微信支付',
                    'uniontid' => $uniontid,
                );
                $res=pdo_update('cqkundian_farm_send_order',$update_save,array('order_number'=>$order_id,'uniacid'=>$log['uniacid']));
            }elseif ($remark==7){  //摘取
                $update_save=array(
                    'status'=>1,
                    'pra_price'=>$fee,
                    'pay_time'=>time(),
                    'pay_method'=>'微信支付',
                    'uniontid' => $uniontid,
                );
                $res=pdo_update('cqkundian_farm_shop_order',$update_save,array('order_number'=>$order_id,'uniacid'=>$log['uniacid']));
            }elseif ($remark==8){  //积分运费支付
                $update_save=array(
                    'status'=>1,
                    'pra_price'=>$fee,
                    'pay_time'=>time(),
                    'pay_method'=>'微信支付',
                    'uniontid' => $uniontid,
                );
                pdo_update('cqkundian_farm_integral_order',$update_save,array('order_number'=>$order_id,'uniacid'=>$log['uniacid']));
            }

            //查询统计记录
            $statistics=pdo_get('cqkundian_farm_statistics',array('uniacid'=>$log['uniacid'],'date'=>date("Ymd",time())));
            if(empty($statistics)){
                $insert=array(
                    'date'=>date("Ymd",time()),
                    'uniacid'=>$log['uniacid'],
                    'order_count'=>1,
                    'total_price'=>$fee,

                );
                pdo_insert('cqkundian_farm_statistics',$insert);
            }else{
                pdo_update('cqkundian_farm_statistics',array('order_count +='=>1,'total_price +='=>$fee),array('uniacid'=>$log['uniacid'],'date'=>date("Ymd",time())));
            }

            if($res){
                cache_delete('kundian_farm_pay_notify_'.$user);
            }
        }
    }

}